package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.GradeDetails;
import com.xmy.cultivate.entity.GradeModule;
import com.xmy.cultivate.entity.GradeOption;
import com.xmy.cultivate.service.IGradeDetailsService;
import com.xmy.cultivate.service.IGradeModuleService;
import com.xmy.cultivate.service.IGradeOptionService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 面试评分模块表 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2024-11-28
 */
@RestController
@RequestMapping("/api/gradeModule")
public class GradeModuleController {

    @Autowired
    IGradeModuleService iGradeModuleService;

    @Autowired
    IGradeDetailsService iGradeDetailsService;

    @Autowired
    IGradeOptionService iGradeOptionService;

    @GetMapping("")
    public Result search() {
        QueryWrapper<GradeModule> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("status");
        queryWrapper.orderByAsc("id");
        List<GradeModule> gradeModuleList = iGradeModuleService.list(queryWrapper);

        return new Result(Code.GET_OK, gradeModuleList, Code.GET_OK_MSG);
    }

    @PostMapping("")
    public Result save(@RequestBody GradeModule gradeModule) {

        iGradeModuleService.saveOrUpdate(gradeModule);

        return new Result(Code.SAVE_OK, null, Code.SAVE_OK_MSG);
    }

    @DeleteMapping("")
    public Result remove(@RequestParam(value = "id") String id) {
        GradeModule gradeModule = iGradeModuleService.getById(id);
        if (Objects.isNull(gradeModule)) {
            return new Result(Code.EXP_ERROR, null, "该数据不存在");
        }

//        // 将考查内容、问题、选项删除
//        List<GradeDetails> gradeDetailsList = iGradeDetailsService.list(new QueryWrapper<GradeDetails>().eq("grade_module_id", id));
//
//        // 删除选项
//        for(GradeDetails gradeDetails :gradeDetailsList){
//            iGradeOptionService.remove(new QueryWrapper<GradeOption>().eq("grade_details_id", gradeDetails.getId()));
//        }
//        // 删除考查内容和问题
//        for(GradeDetails gradeDetails :gradeDetailsList){
//            iGradeDetailsService.remove(new QueryWrapper<GradeDetails>().eq("grade_module_id", gradeDetails.getId()));
//        }

        // 删除模块
        iGradeModuleService.removeById(id);

        return new Result(Code.DELETE_OK, null, Code.DELETE_OK_MSG);
    }


}
